package org.smile.jstl.tags.sql;

import java.sql.Connection;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.smile.db.DbManager;
import org.smile.db.DbUtils;
import org.smile.json.JSONArray;

public class JsonTag extends TagSupport {
	/**
	 * 查询语句
	 */
	private String sql;
	/**
	 * 数据源名称
	 */
	private String dataSourceName;
	public int doStartTag() throws JspException {
		Connection conn;
		if(dataSourceName!=null){
		   conn=DbManager.getConnection(dataSourceName);
		}
		else{
			conn=DbManager.getConnection();
		}
		try {
			List list=DbUtils.query(conn, sql);
			pageContext.getOut().println(JSONArray.toJSONString(list));
		} catch (Exception e) {
			throw new JspException("sql options tag cased a exception :",e);
		}
		return SKIP_BODY;
	}
	
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public String getDataSourceName() {
		return dataSourceName;
	}
	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}
	
	
}
